﻿using System;
using System.Globalization;

namespace TableTypes
{
    public enum ParseType
    {
        BookTable,
        ReservedTable,
        WaitListTable,
        AttendedTable,
        CancelledTable
    }

    [Serializable]
    public class ClassTime
    {
        TimeSpan m_begin = new TimeSpan(0,0,0);
        public TimeSpan Begin
        {
            get
            {
                return m_begin;
            }
        }

        TimeSpan m_end = new TimeSpan(0, 0, 0);
        public TimeSpan End
        {
            get
            {
                return m_end;
            }
        }

        DateTime m_date = new DateTime();
        public DateTime Date
        {
            get
            {
                return m_date;
            }
        }

        public ClassTime(DateTime date, TimeSpan begin, TimeSpan end)
        {
            m_date = date;
            m_begin = begin;
            m_end = end;
        }

        public static ClassTime ParseAttendedEntry(string strDate, string strBeginTime, string strEndTime)
        {
            /* 09-11-05 
             * 09:00:00 
             * 10:00:00*/
            DateTime date = DateTime.ParseExact(strDate, "yy-MM-dd", CultureInfo.InvariantCulture);

            DateTime zeroDate  = DateTime.Now.Date;
            DateTime beginTime = DateTime.ParseExact(strBeginTime, "HH:mm:ss", CultureInfo.InvariantCulture);
            DateTime endTime   = DateTime.ParseExact(strEndTime,   "HH:mm:ss", CultureInfo.InvariantCulture);

            return new ClassTime(date, beginTime - zeroDate, endTime - zeroDate);
        }

        public static ClassTime ParseCancelledEntry(string strDate, string strBeginTime, string strEndTime)
        {
            /* Oct 20 2009 12:00:00:000AM
             * 09:00:00 
             * 10:00:00*/

            string[] dateParts = strDate.Split(' ');
            string strOriginalDate = dateParts[0] + " " + dateParts[1] + " " + dateParts[2];

            DateTime date = DateTime.ParseExact(strOriginalDate, "MMM dd yyyy", CultureInfo.InvariantCulture);

            DateTime zeroDate = DateTime.Now.Date;
            DateTime beginTime = DateTime.ParseExact(strBeginTime, "HH:mm", CultureInfo.InvariantCulture);
            DateTime endTime = DateTime.ParseExact(strEndTime, "HH:mm", CultureInfo.InvariantCulture);

            return new ClassTime(date, beginTime - zeroDate, endTime - zeroDate);
        }

        public static ClassTime ParseReservedEntry(string dateTimeSlot)
        {
            /*Wednesday 11/11/2009 13:00 - 14:00*/

            string[] parts = dateTimeSlot.Split(' ');
            string strDate = parts[1].Trim();            

            DateTime date = DateTime.ParseExact(strDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);

            string begin = parts[2].Trim();
            string end = parts[4].Trim();

            DateTime zeroDate = DateTime.Now.Date;
            DateTime beginTime = DateTime.ParseExact(begin, "HH:mm", CultureInfo.InvariantCulture);
            DateTime endTime = DateTime.ParseExact(end, "HH:mm", CultureInfo.InvariantCulture);

            return new ClassTime(date, beginTime - zeroDate, endTime - zeroDate);
        }

        public static ClassTime ParseWaitListEntry(string dateTimeSlot)
        {
            /*Wednesday 11/11/2009*/

            string[] parts = dateTimeSlot.Split(' ');

            DateTime date = DateTime.ParseExact(parts[1], "dd/MM/yyyy", CultureInfo.InvariantCulture);
            return new ClassTime(date, new TimeSpan(), new TimeSpan());
        }

        public static ClassTime ParseBookEntry(string dateTimeSlot)
        {
            /* Nov 10 2009 12:30 - 13:30 */
            string[] parts = dateTimeSlot.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
            string strDate = parts[0] + " " + parts[1] + " " + parts[2];

            DateTime date = DateTime.ParseExact(strDate, "MMM d yyyy", CultureInfo.InvariantCulture);

            string begin = parts[3].Trim();
            string end = parts[5].Trim();

            DateTime zeroDate = DateTime.Now.Date;
            DateTime beginTime = DateTime.ParseExact(begin, "HH:mm", CultureInfo.InvariantCulture);
            DateTime endTime = DateTime.ParseExact(end, "HH:mm", CultureInfo.InvariantCulture);

            return new ClassTime(date, beginTime - zeroDate, endTime - zeroDate);
        }

        public override string ToString()
        {
            string date = m_date.ToShortDateString() ;
            string timeFormat ="{0,2:00}:{1,2:00}";
            string begin = String.Format(timeFormat, m_begin.Hours, m_begin.Minutes);
            string end = String.Format(timeFormat , m_end.Hours, m_end.Minutes);

            return date + " " + begin + " - " + end;
        }
    }
}
